package ttOrder.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ttOrder.dao.inter.OrderDetailMapper;
import ttOrder.model.po.OrderDetail;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;
import ttOrder.dao.inter.OrderDetailMapper;
import ttOrder.model.po.Food;
import ttOrder.model.po.OrderDetail;
import ttOrder.model.vo.FoodVO;
import ttOrder.model.vo.OrderDetailVO;

@Service
@Transactional
public class OrderDetailServiceImpl extends abstractService<OrderDetail>{

	@Autowired
	private OrderDetailMapper orderDetailMapper;
	
	@Autowired
	FoodServiceImpl foodService;
	
	/* operate with vo class */
	
	/**
	 * 根据当前实体类的OrderId字段获取OrderDetailVO集合
	 * @author emoker 
	 * @see selectByExample(T t)
	 * */
	public List<OrderDetailVO> getDetailVOByOrderId(OrderDetail record) {
		/*
		先获取OrderDetail的PO类list，然后将一个一个元素转化为VO类，同时查询VO类所需的各个实体类
		*/
		List<OrderDetailVO> volist = new ArrayList<OrderDetailVO>();
		List<OrderDetail> polist = getDetailByOrderId(record);
//		System.out.println(polist.size());
		OrderDetailVO vo = null;
		for (OrderDetail po : polist) {
			vo = new OrderDetailVO();
			BeanUtils.copyProperties(po, vo);
			vo.setFood(foodService.getByKey(new FoodVO(po.getFoodId())));
			vo.setFoodMoney(vo.getFoodCount()*vo.getFood().getPrice());
			volist.add(vo);
		}
		return volist;
	}
	
	/* operate with po class */
	
	@Override
	public List<OrderDetail> getAll() {
		return orderDetailMapper.selectAll();
	}

	@Override
	public List<OrderDetail> getByEntity(OrderDetail record) {
		return orderDetailMapper.select(record);
	}

	@Override
	public OrderDetail getByKey(OrderDetail key) {
		return orderDetailMapper.selectByPrimaryKey(key);
	}

	@Override
	public int insert(OrderDetail record) {
		if (record.getOrderId() > 0) {
			
		}
		return orderDetailMapper.insert(record);
	}

	@Override
	public int update(OrderDetail record) {
		return orderDetailMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int deleteByKey(OrderDetail key) {
		return orderDetailMapper.deleteByPrimaryKey(key);
	}

	@Override
	public int deleteByEntity(OrderDetail record) {
		return orderDetailMapper.delete(record);
	}

	@Override
	public int getCount(OrderDetail record) {
		return orderDetailMapper.selectCount(record);
	}
	
	/**
	 * 根据当前实体类的OrderId字段获取OrderDetail集合
	 * @author emoker 
	 * @see selectByExample(T t)
	 * */
	public List
	<OrderDetail> getDetailByOrderId(OrderDetail record) {
		Example e = new Example(OrderDetail.class); 
		Example.Criteria criteria = e.createCriteria();
		if (record.getOrderId() > 0) {
			criteria.andEqualTo("orderId", record.getOrderId());
		}
		return orderDetailMapper.selectByExample(e);
	}
	
	List<OrderDetailVO> trunListPoToVo(List<OrderDetail> polist){
		List<OrderDetailVO> volist = new ArrayList<OrderDetailVO>();
		if (polist.size() > 0) {
			OrderDetailVO vo = null;
			for (OrderDetail po : polist) {
				vo = new OrderDetailVO();
				BeanUtils.copyProperties(po, vo);
				volist.add(vo);
			}
		}
		return volist;
	}
	
}
